require('dotenv').config();
const glob = require('glob')
const fs = require('fs')
const path = require('path')
const config = require('config')
const nextcloudDataDir = config.get('nextcloud.dataDir')
import {findUsers} from './utils/nc/user'
import {loadGraphConcept} from "./route/v2/concept";

const sync = async ()=>{
    let globAync = async (path)=> {
        return new Promise((resolve, reject) => {
            glob(path,{},function(err,files){
                resolve(files)
            })
        })
    }
    let users = await findUsers(),files,data
    for(let user of users){
        files = await globAync(nextcloudDataDir + '/' + user + '/files/**/*.lnkg',{})
        for(let file of files){
            data = fs.readFileSync(file,'utf-8')
            data = JSON.parse(data)
            data.title = path.basename(file,'.lnkg')
            await loadGraphConcept(data)
            console.log(`file ${file} sync finished`);
        }
        console.log(`user ${user} sync finished`);
    }
}

if (require.main === module) {
    sync().then(() => {
            console.log('all data sync finished');
            process.exit(0);
        }
    ).catch((err) => {
        console.log(err)
    })
}
